package com.situ.storehouse.controller;

import java.io.IOException;
import java.time.LocalDateTime;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.storehouse.entity.Attendant;
import com.situ.storehouse.entity.Bill;
import com.situ.storehouse.entity.Wares;
import com.situ.storehouse.service.BillService;
import com.situ.storehouse.service.BillServiceImpl;
import com.situ.storehouse.service.WaresService;
import com.situ.storehouse.service.WaresServiceImpl;
import com.situ.storehouse.util.BaseController;
import com.situ.storehouse.vo.BillVO;
import com.situ.storehouse.vo.LayuiTableVO;
//出入库单据管理控制类
@WebServlet("/billcontroller")
public class BillController extends BaseController {
	private static final long serialVersionUID = 1L;
	BillService service=new BillServiceImpl();
	//打开出入库单据管理界面
	public void page(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("/WEB-INF/bill.jsp").forward(req, resp);
	}
	//往表格中放数据
		public LayuiTableVO list(Integer page,Integer limit,String keyword,String maindate){
			List<BillVO> list=service.select(page,limit,keyword,maindate);
			LayuiTableVO vo=new LayuiTableVO();
			vo.setCode(0);
			vo.setData(list);
			vo.setCount(service.count(keyword,maindate));
			return vo;
		}
		
		//查询单据信息
		public List<Wares> waresSelect(HttpServletRequest request, HttpServletResponse response){
			WaresService service=new WaresServiceImpl();
			List<Wares> list=service.selectAll();
			request.setAttribute("wares", list);
			return list;
			
		}
		//查询某个单据信息
		public Bill select(Integer id){
			return service.select(id);
			}

		//增加+修改单据信息
		@SuppressWarnings("unlikely-arg-type")
		public void save(Bill bill,HttpSession session) {
			bill.setTime(LocalDateTime.now());//自动生成创建时间
			//通过session 获取是谁创建的单据
			Object obj=session.getAttribute("currentAttendant");
			Attendant attendant =(Attendant)obj;
			bill.setCreatby(attendant.getId());
			
			if(bill.getId()==null || bill.getId().equals("")) {
				service.add(bill);
			}else {
				bill.setId(bill.getId());
				service.update(bill);
			}
		}
}
